package com.yao2san.simcasclient.authentication.basics.filter;

import com.yao2san.sim.framework.utils.BeanContextUtil;
import com.yao2san.sim.framework.utils.WebUtil;
import com.yao2san.simcasclient.authentication.basics.Constants;
import com.yao2san.simcasclient.config.ClientConfig;
import com.yao2san.simcasclient.config.ServerConfig;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class SimCasBasicsFilter implements Filter {
    private ClientConfig clientConfig;
    private ServerConfig serverConfig;

    public SimCasBasicsFilter(ClientConfig clientConfig, ServerConfig serverConfig) {
        this.clientConfig = clientConfig;
        this.serverConfig = serverConfig;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        if (matchExcludeUrls(request)) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        if (checkSessionCookie(request, response)) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        ServerConfig serverConfig = BeanContextUtil.getBean(ServerConfig.class);
        StringBuilder url = new StringBuilder();
        url.append("http://")
                .append(serverConfig.getHost())
                .append(":")
                .append(serverConfig.getPort())
                .append(serverConfig.getLoginUrl())
                .append("?service=")
                .append(request.getRequestURL());
        response.sendRedirect(url.toString());
    }

    /**
     * match should be exclude urls,if matches, return true
     *
     * @param request a request
     * @return if matches, return true,else return false and will not need authentication
     */
    private boolean matchExcludeUrls(HttpServletRequest request) {
        String url = request.getRequestURI();
        List<String> urlExcludes = clientConfig.getUrlExcludes();
        return WebUtil.mathUrl(urlExcludes, url);
    }

    @Override
    public void destroy() {

    }

    private boolean checkSessionCookie(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return false;
        }
        for (Cookie cookie : cookies) {
            if (Constants.JSESSIONID.equals(cookie.getName())) {
                HttpSession session = request.getSession();
                if (session.getAttribute(Constants.JSESSIONID) != null) {
                    return true;
                }
            }
        }
        return false;
    }
}
